﻿using AutoMapper;
using AutoMapper.Mappers;
using Core.Dtos;
using Core.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Core.Mappers;
using Core.Messages;

namespace Tests.Mappers
{
    [TestClass]
    public class MapListTest
    {
        public IList<Log>  CreateLogsList()
        {
            Log log = new Log() { Id = 1, Message = "Message", Exception = "Exception" };                                                
            IList<Log> logs = new List<Log>();
            logs.Add(log);

            return logs;
        }

        [TestMethod]
        public void Automapper_Can_Convert_Lists()
        {   
            //Arrange
            AutoMapper.Mapper.CreateMap<Log, LogDto>();
            var logs = CreateLogsList();
            
            //Act
            var dtos = logs.Map<Log, LogDto>();
            
            //Assert            
            Assert.AreEqual(dtos[0].Id, logs[0].Id);
            Assert.AreEqual(dtos[0].Message, logs[0].Message);
        }

        [TestMethod]
        public void Automapper_Can_Convert_ListResponse()
        {
            //Arrange
            AutoMapper.Mapper.CreateMap<Log, LogDto>();
            AutoMapper.Mapper.CreateMap<QueryResponse<Log>, QueryResponse<LogDto>>()
               .ConvertUsing(new ListTypeConverter<Log, LogDto>());
            var logs = CreateLogsList();
            QueryResponse<Log> response = new QueryResponse<Log>() { List = logs };            

            //Act
            QueryResponse<LogDto> responseDto = Mapper.Map<QueryResponse<Log>, QueryResponse<LogDto>>(response);
            
            //Assert
            IList<LogDto> dtos = responseDto.List;
            Assert.AreEqual(dtos[0].Id, logs[0].Id);
            Assert.AreEqual(dtos[0].Message, logs[0].Message);
            Assert.AreEqual(dtos[0].Exception, logs[0].Exception);
        }

        [TestMethod]
        public void Automapper_Can_Convert_ListResponse_With_Custom_Mapping()
        {
            //Arrange
            AutoMapper.Mapper.CreateMap<Log, LogDto>();
            AutoMapper.Mapper.CreateMap<QueryResponse<Log>, QueryResponse<LogDto>>()
                .ConvertUsing(new LogListTypeConverter());
            var logs = CreateLogsList();
            QueryResponse<Log> response = new QueryResponse<Log>() { List = logs };

            //Act
            QueryResponse<LogDto> responseDto = Mapper.Map<QueryResponse<Log>, QueryResponse<LogDto>>(response);
            LogDto dto = responseDto.List[0];

            //Assert
            IList<LogDto> dtos = responseDto.List;
            Assert.AreEqual(dtos[0].Id, logs[0].Id);
            Assert.AreEqual(dtos[0].Message, logs[0].Message);
            Assert.IsNull(dtos[0].Exception);
        }               
    }    
}
